const models = require('../models');
const moment = require('moment')
const a = moment(moment().format('YYYY-MM-DD')).subtract(3,'hours')
console.log(a)
class ClassController {
  constructor(){
  }
  async findAll(whereObj,columnsArr,date){
    var result = {};
    if(whereObj.name||whereObj.department){
      result.where = whereObj
    }else{
      result.where = {}
    }
    // 今天的九点
    const compareTime = moment(moment().format('YYYY-MM-DD')).add(21,'hours')
    // 每个date显示的都是是从前一天的晚上九点到第二天晚上九点，如果date是今天，那么判断是否在晚上九点之前
    let ltTime = moment(date).add(21, 'hours') //big
    let gtTime = moment(date).subtract(3, 'hours') //small
    if(date===moment().format('YYYY-MM-DD')){
      // date 是今天
      if(moment()>compareTime){
        // 超过当天九点，
        ltTime = moment(date).add(24, 'hours') //big
        gtTime = moment(date).subtract(21, 'hours') //small
      }
    }
    
    result.include = [{
      model: models.classrecord,
      as: 'classrecords',
      where: {
        time: {
          $lt: ltTime,
          $gt: gtTime
        }
      },
      required: false
    }]
    if(columnsArr.length){
      result.attributes = columnsArr;
    }
    const records = await models.Class.findAll(
      result
    );
    return records;
  }
}

module.exports = new ClassController()